/**
 * @author iLakeYC
 */
var debugMode = false;
+function ($) {
    "use strict";

    /**
     * Before use. Import <code>storage-utils.js</code> first.
     * */
    var alertTimer = null;
    $.ajaxRequest = function (param) {
          var a=JSON.stringify(param.data);
          var now = Date.now();
          param.headers = param.headers || {};
          param.headers.ims = window.localStorage.getItem('ACCESS_IMS') || (window.localStorage.setItem('ACCESS_IMS',now),now);
          if ( debugMode ) {
              console.log('Request:  url: "' + param.url + '" ===> param: ', param.data)
          }
			var async = param.async || true;
			var base=new Base64();
			var baseParam=(base.encode(a));
			var md5Param=(md5(a));
            var str=md5Param+','+baseParam;
            // var strUrl="https://newapi.padj.group/";
            var strUrl="http://newapi.t.padj.group/";
            //var strUrl="http://192.168.1.148:8080/jeeplus/";
            //var strUrl="http://192.168.1.79:8000/jeeplus";
            //var strUrl="http://192.168.1.155:8080/jeeplus/";
            // var strUrl="http://192.168.1.38:8080/jeeplus/";
            //var strUrl="http://118.190.32.16:8070/jeeplus/"
            //var strUrl="http://wx.padj.group/wechart/"
            // var strUrl="http://192.168.1.162:8080/jeeplus/"
            //var strUrl="http://192.168.1.158:8080/jeeplus/";
            //var strUrl="http://192.168.1.216:8080/jeeplus/";
            //var strUrl="http://192.168.1.77:8080/jeeplus/";
            // var pageBaseUrl ='http://weweb.t.padj.group/'
        $.ajax({
            url: strUrl+param.url,
            timeout: param.timeout || 10000,
            type: param.type,
            data: str,
            async:async,
            contentType:"text/plain",
            dataType:"text",
            beforeSend: function (request) {
                var ims = $.pacefulIms.get();
                if ($.pacefulUser.tokenIsNotEmpty()) {
                    request.setRequestHeader("tk", $.pacefulUser.accessToken());
                };
                request.setRequestHeader("userType","wx");
                if (param.headers) {
                    for (var k in param.headers) {
                        request.setRequestHeader(k, param.headers[k]);
                    }
                }
            },
            success: function (response) {
                var responseT=response.split(","); 
                var response1=base.decode(responseT[1]);
                var response2=JSON.parse(base.decode(responseT[1]));
                if(md5(response1)==responseT[0]){
                    var res = JSON.parse(response1)
                    console.log('Response:  url: "' + param.url + '" ===>', res);
                    if ( res.code === '200001') { // 登录失效
                        setTimeout(function(){
                            wx.closeWindow();
                        },2000)
                        alert(res.msg);
                        location.href = "/#/login";
                        // clearTimeout(alertTimer)
                        // alertTimer = setTimeout(function(){
                        //     alert(res.msg)
                        // },1000)
                    } else if (res.code === '100099') {
                        $("#over").css("display","none");
                        $("#layout").css("display","none");
                        var resdata = JSON.parse(res.msg);
                        $("#res_box").show();
                        $("#res_box p").html(resdata.content);
                        var text_msg = "";
                        if (resdata.type == 1) {
                             // url
                             text_msg = '<a href ="'+resdata.url+'">'+resdata.sureText+'</a>';
                        } else {
                            // phone
                            text_msg = '<a href =tel:"'+resdata.phone+'">'+resdata.sureText+'</a>';
                        }
                        $("#contact_kf").html(text_msg);
                    }
                    if (param.complete) {param.complete()};
                    if (param.success){ param.success(response2)};
                }else{
                    alert("含有不合法字符");
                }     
            },
            error: function (message) {
                if (param.complete) param.complete();
                if (param.error) param.error(message);
            }
        })
    };

    $.ajaxGet = function (param) {
        $.ajaxRequest({
            url: param.url,
            type: "GET",
            data: param.data,
            success: function (response) {
                if (param.complete != null) {
                    param.complete();
                }
                if (response.code == '000000' || response.code == 'success') {
                    if (param.success != null) {
                        param.success(response.data);   
                    }
                } else {
                    if (param.error != null) {
                        // param.error(response.msg);
                    }
                }
            },
            error: function (errorMessage) {
                
                if (param.complete != null) {
                    param.complete();
                }
                if (param.error != null) {
                    param.error('无法连接到服务器，请稍后再试');
                }
            }
        })
    };

    $.ajaxPost = function (param) {
        if (!param.data) {
            param.data = {};
        }
        param.data["ims"] = $.pacefulIms.get();
        if ($.pacefulUser.tokenIsNotEmpty()) {
            param.data["tk"] = $.pacefulUser.accessToken();
        }
        $.ajaxRequest({
            url: param.url,
            type: "POST",
            data: param.data,
            success: function (response) {
                if (param.complete != null) {
                    param.complete();
                }
                if (response.code == '000000' || response.code == 'success' || response.code== '404404') {
                    if (param.success != null) {
                        if (response.data) { // 有data时返回response.data 没有时返回response
                            param.success(response.data);
                        } else {
                            param.success(response); 
                        }
                    }
                } else {
                    if (param.error != null) {
                        // param.error(response.msg);
                    }
                }
            },
            error: function (errorMessage) {
                if (param.complete != null) {
                    param.complete();
                }
                if (param.error != null) {
                    param.error('无法连接到服务器，请稍后再试');
                }
            }
        })
    }


    $.GET = function (url, data) {
        var completeHandler;
        $.ajaxGet({
            url: url,
            data: data,
            success: function (response) {
                if (completeHandler && completeHandler.success) completeHandler.success(response)
            },
            error: function (errorMessage) {
                if (completeHandler && completeHandler.error) completeHandler.error(errorMessage)
            },
            complete: function () {
                if (completeHandler && completeHandler.complete) completeHandler.complete();
            }
        });
        return function (c) {
            completeHandler = c;
        };
    };

    $.POST = function (url, data) {
        var completeHandler;
        $.ajaxPost({
            url: url,
            data: data,
            success: function (response) {
                if (completeHandler && completeHandler.success) completeHandler.success(response)
            },
            error: function (errorMessage) {
                if (completeHandler && completeHandler.error) completeHandler.error(errorMessage)
            },
            complete: function () {
                if (completeHandler && completeHandler.complete) completeHandler.complete();
            }
        });
        return function (c) {
            completeHandler = c;
        };
    };

}($);
